package com.winona.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.taplinker.client.R;
import com.taplinker.core.util.json.JsonUtil;
import com.winona.ConstantValue;
import com.winona.bean.User;
import com.winona.domain.DataCenter;
import com.winona.ui.LoginActivity;

public class SomeUtil {
	
	private static void startActivity(Activity activity, Class<?> intentClazz) {
		try {
			activity.startActivity(new Intent(activity, intentClazz));
			activity.overridePendingTransition(R.anim.input, R.anim.out);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static boolean isLoginUser(Activity activity) {
		if (DataCenter.getInstance().getUser() != null) {
			return true;
		}
		try {
			Toast.makeText(activity, "您尚未登录，请先登录吧", Toast.LENGTH_SHORT).show();
			startActivity(activity, LoginActivity.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean hasNewVersion(String remoteAppVersion) {
		try {
			if (DataCenter.getInstance().getCurrentAppVersion().compareToIgnoreCase(remoteAppVersion) < 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
//	public static void downloadRemoteApp(Context ctx) {
//		try {
//			Uri uri = Uri.parse(AppConfigContext.URL_DOWNLOAD_REMOTE_APK);
//			Intent it = new Intent(Intent.ACTION_VIEW, uri);
//			it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			ctx.getApplicationContext().startActivity(it);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	
	public static void saveUserInfo(Context ctx, User user) {
		try {
			SharedPreferences sp = ctx.getSharedPreferences(ConstantValue.FILE_AITER, Context.MODE_PRIVATE);
			Editor editor = sp.edit();
			editor.putString(ConstantValue.KEY_LOGIN_USER, JsonUtil.objectToJsonStr(user));
			editor.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static User getLoaclUserInfo(Context ctx) {
		User user = null;
		try {
			SharedPreferences sp = ctx.getSharedPreferences(ConstantValue.FILE_AITER, Context.MODE_PRIVATE);
			user = JsonUtil.jsonToObject(sp.getString(ConstantValue.KEY_LOGIN_USER, ""), User.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}
	
	public static String timeFormat(long time, String style) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(style);
			return sdf.format(new Date(time));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	public static void hideKeyboard(Activity activity) {
    	if(activity == null){
    		return;
    	}
    	InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    	if(imm != null && activity.getCurrentFocus() != null){
    		imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    	}
    }
}
